<template>
  <div class="station-container">
    <div class="header">
      <h1 class="title">车站信息管理</h1>
      <p class="subtitle">管理铁路系统中的车站信息，包括站名、拼音及首字母</p>
    </div>


    <a-card class="operation-card">
      <div class="operation-bar">
        <a-space>
          <a-button type="primary" @click="handleQuery()" class="refresh-btn">
            <template #icon><reload-outlined /></template>
            刷新数据
          </a-button>
          <a-button type="primary" @click="onAdd" class="add-btn">
            <template #icon><plus-outlined /></template>
            新增车站
          </a-button>
        </a-space>
        <div class="statistics">
          <div class="stat-item">
            <dashboard-outlined />
            <span>车站总数: {{ pagination.total }}</span>
          </div>
        </div>
      </div>
    </a-card>

    <a-card class="table-card">
      <a-table
          :dataSource="stations"
          :columns="columns"
          :pagination="pagination"
          @change="handleTableChange"
          :loading="loading"
          :locale="{ emptyText: '暂无车站数据' }"
          rowKey="id"
          bordered
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'name'">
            <div class="station-name">
              <home-outlined class="station-icon" />
              <span>{{ record.name }}</span>
            </div>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-space class="operation-buttons">
              <a-tooltip title="编辑车站信息">
                <a-button type="primary" size="small" @click="onEdit(record)" class="edit-btn">
                  <template #icon><edit-outlined /></template>
                </a-button>
              </a-tooltip>
              <a-popconfirm
                  title="确定要删除此车站吗？"
                  ok-text="确认"
                  cancel-text="取消"
                  @confirm="onDelete(record)"
              >
                <a-tooltip title="删除车站">
                  <a-button type="primary" danger size="small" class="delete-btn">
                    <template #icon><delete-outlined /></template>
                  </a-button>
                </a-tooltip>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <a-modal
        v-model:visible="visible"
        :title="station.id ? '编辑车站信息' : '添加新车站'"
        @ok="handleOk"
        ok-text="确认"
        cancel-text="取消"
        :width="600"
        centered
        class="station-modal"
    >
      <a-form :model="station" :label-col="{span: 6}" :wrapper-col="{ span: 16 }">
        <a-form-item label="站名" required>
          <a-input v-model:value="station.name" placeholder="请输入车站名称" allow-clear>
            <template #prefix><home-outlined /></template>
          </a-input>
        </a-form-item>
        <a-form-item label="站名拼音" required>
          <a-input v-model:value="station.namePinyin" disabled placeholder="车站拼音全称" allow-clear>
            <template #prefix><font-size-outlined /></template>
          </a-input>
        </a-form-item>
        <a-form-item label="拼音首字母" required>
          <a-input v-model:value="station.namePy" disabled placeholder="车站拼音首字母" allow-clear>
            <template #prefix><font-colors-outlined /></template>
          </a-input>
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button key="back" @click="visible = false">取消</a-button>
        <a-button key="submit" type="primary" @click="handleOk">提交</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted,watch } from 'vue';
import { notification } from "ant-design-vue";
import axios from "axios";
import {pinyin} from "pinyin-pro";
import {
  ReloadOutlined, PlusOutlined, EditOutlined,
  DeleteOutlined, HomeOutlined, DashboardOutlined,
  FontSizeOutlined, FontColorsOutlined
} from '@ant-design/icons-vue';

export default defineComponent({
  name: "station-view",
  components: {
    ReloadOutlined,
    PlusOutlined,
    EditOutlined,
    DeleteOutlined,
    HomeOutlined,
    DashboardOutlined,
    FontSizeOutlined,
    FontColorsOutlined
  },
  setup() {
    const visible = ref(false);
    let station = ref({
      id: undefined,
      name: undefined,
      namePinyin: undefined,
      namePy: undefined,
      createTime: undefined,
      updateTime: undefined,
    });
    const stations = ref([]);
    const pagination = ref({
      total: 0,
      current: 1,
      pageSize: 10,
    });
    let loading = ref(false);
    const columns = [
      {
        title: '站名',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '站名拼音',
        dataIndex: 'namePinyin',
        key: 'namePinyin',
      },
      {
        title: '站名拼音首字母',
        dataIndex: 'namePy',
        key: 'namePy',
      },
      {
        title: '操作',
        dataIndex: 'operation',
        align: 'center',
        width: '150px'
      }
    ];
    watch(() => station.value.name, ()=>{
      if (Tool.isNotEmpty(station.value.name)) {
        station.value.namePinyin = pinyin(station.value.name, { toneType: 'none'}).replaceAll(" ", "");
        station.value.namePy = pinyin(station.value.name, { pattern: 'first', toneType: 'none'}).replaceAll(" ", "");
      } else {
        station.value.namePinyin = "";
        station.value.namePy = "";
      }
    }, {immediate: true});

    const onAdd = () => {
      station.value = {};
      visible.value = true;
    };

    const onEdit = (record) => {
      station.value = window.Tool.copy(record);
      visible.value = true;
    };

    const onDelete = (record) => {
      axios.delete("/business/admin/station/delete/" + record.id).then((response) => {
        const data = response.data;
        if (data.success) {
          notification.success({
            message: '删除成功',
            description: `车站 ${record.name} 已删除`,
          });
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize,
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleOk = () => {
      axios.post("/business/admin/station/save", station.value).then((response) => {
        let data = response.data;
        if (data.success) {
          notification.success({
            message: '保存成功',
            description: station.value.id ? '车站信息已更新' : '新车站已添加',
          });
          visible.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleQuery = (param) => {
      if (!param) {
        param = {
          page: 1,
          size: pagination.value.pageSize
        };
      }
      loading.value = true;
      axios.get("/business/admin/station/query-list", {
        params: {
          page: param.page,
          size: param.size
        }
      }).then((response) => {
        loading.value = false;
        let data = response.data;
        if (data.success) {
          stations.value = data.content.list;
          pagination.value.current = param.page;
          pagination.value.total = data.content.total;
        } else {
          notification.error({description: data.message});
        }
      }).catch(() => {
        loading.value = false;
      });
    };

    const handleTableChange = (pagination) => {
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      station,
      visible,
      stations,
      pagination,
      columns,
      handleTableChange,
      handleQuery,
      loading,
      onAdd,
      handleOk,
      onEdit,
      onDelete
    };
  },
});
</script>

<style scoped>
.station-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.header {
  margin-bottom: 24px;
  text-align: center;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #1f2f3d;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  color: #606266;
  font-size: 16px;
}

.operation-card {
  margin-bottom: 24px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.refresh-btn, .add-btn {
  border-radius: 6px;
  font-weight: 500;
  padding: 0 20px;
  height: 36px;
}

.add-btn {
  background: linear-gradient(90deg, #13c2c2, #1890ff);
  border: none;
}

.statistics {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f0f9ff;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  color: #1890ff;
}

.station-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.station-icon {
  color: #1890ff;
  font-size: 16px;
}

.operation-buttons {
  display: flex;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(90deg, #52c41a, #389e0d);
  border: none;
}

.delete-btn {
  border: none;
}

:deep(.ant-table-thead > tr > th) {
  background-color: #f0f9ff !important;
  font-weight: 600;
  color: #1f2f3d;
}

:deep(.ant-table-row) {
  transition: background-color 0.3s;
}

:deep(.ant-table-row:hover) {
  background-color: #f0f9ff !important;
}

.station-modal :deep(.ant-modal-header) {
  border-radius: 10px 10px 0 0;
}

.station-modal :deep(.ant-modal-body) {
  padding: 24px;
}

.station-modal :deep(.ant-form-item) {
  margin-bottom: 20px;
}

.station-modal :deep(.ant-input) {
  border-radius: 6px;
  height: 40px;
}

@media (max-width: 768px) {
  .operation-bar {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .station-container {
    padding: 15px;
  }

  .title {
    font-size: 24px;
  }
}
</style>